package com.xyhuo.leetcode.january;

import com.xyhuo.leetcode.common.TreeNode;

/**
 * 二叉树的最小深度
 * https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/
 *
 * @time 2024/1/10
 */
public class MinDepth {

    public int minDepth(TreeNode root) {
        return search(root);
    }

    private int search(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        int depth = Integer.MAX_VALUE;
        if (root.left != null) {
            int leftDepth = search(root.left);
            if (leftDepth < depth) {
                depth = leftDepth;
            }
        }
        if (root.right != null) {
            int rightDepth = search(root.right);
            if (rightDepth < depth) {
                depth = rightDepth;
            }
        }
        return depth + 1;
    }
}
